<?php

require_once('../../connection/_dbconnect_.php');
//require_once('m_activity_log.php');

function getSwaps($username){
		
	$parentArray = array();
	
	$today = new DateTime('NOW');
	$today->setTimeZone(new DateTimeZone("America/Costa_Rica"));
	$timestamp = strtotime($today->format('Y-m-d'));
	$date = date('Y-m-d', strtotime('monday this week', $timestamp));
	$dateArray = explode('-', $date);		
	
	$sql = "select qr.idqshift_change_request,
				   qr.requesting_user,
				   qr.current_qshift,
				   qs1.description as desc1,
				   qr.target_user,
				   qr.target_qshift,
				   qs2.description as desc2,
				   qr.day_requested,
				   qr.request_status
			from qshift_change_request qr
			join queue_shift qs1 on (qs1.idqueue_shift = qr.current_qshift)
			join queue_shift qs2 on (qs2.idqueue_shift = qr.target_qshift)
				where (qr.requesting_user = '".$username."' or qr.target_user= '".$username."')
				and (qr.day_requested between '".$dateArray[0]."-".$dateArray[1]."-".$dateArray[2]."' 
											and '".$dateArray[0]."-".$dateArray[1] ."-".($dateArray[2] + 4)."')  			
			order by qr.day_requested DESC, qr.request_status DESC;";
			
	//echo $sql;exit;
	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	
	if (!$result) 
		return false;
	else if (mysql_num_rows($result)==0) {
		echo "<h1> No data to show for user:".$username."</h1>";
		exit;
	} else {
		while($obj = mysql_fetch_object($result)){
			$childArray = array();		
			$childArray[] = $obj->idqshift_change_request;
			$childArray[] = $obj->requesting_user;
			$childArray[] = $obj->current_qshift;
			$childArray[] = $obj->desc1;
			$childArray[] = $obj->target_user;
			$childArray[] = $obj->target_qshift;
			$childArray[] = $obj->desc2;
			$childArray[] = $obj->day_requested;
			$childArray[] = $obj->request_status;
			
			$parentArray[]= $childArray;
		}
	}

	return $parentArray;
}
  
function cancelSwap($swapId,$username){
	
	$today = new DateTime('NOW');
	$today->setTimeZone(new DateTimeZone("America/Costa_Rica"));
	$date = $today->format('Y-m-d');
	
	$sql = "select t.day_requested as date
			from qshift_change_request t
			where t.idqshift_change_request='".$swapId."';";

	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	
	if (!$result) {
		return false;
	} else {
	
		$obj = mysql_fetch_object($result);
	
		//echo "date = $date and obj date = $obj->date";exit;
	
		if ($date <= $obj->date) {			
			
			$sql = "update qshift_change_request set request_status='Cancelled by ".$username."' where idqshift_change_request='".$swapId."';";
			
			$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
			
			if (!$result) {
				return false;
			} else {
					
				//$result=addLogEntry($date->format('Y-m-d'), "Schedule Change Request=".$id." was denied");	
				return true;
			}
		} else {
			echo "<h1> Error: The SWAP is too old to be cancelled!</h1><br><br>
				  <h3>...you can only cancel Swaps from today or later days in the week.</h3><br>";
			exit;
		}
	}
}




?>